import os
import time
import threading


if os.name == "nt":     # nt 是Windows的代號

    MEDIA_MOUNT_1 = "D:\\mysite\\fileData_master\\formal\\ST\\"         # 主文件服务器挂载盘
    MEDIA_MOUNT_2 = "D:\\mysite\\fileData_slave\\formal\\ST\\"          # 挂载文件服务器挂载盘
    if not os.path.exists(MEDIA_MOUNT_1):                               # Windows环境下不存在会创建相应的文件夹
        os.makedirs(MEDIA_MOUNT_1)

else:                   # 服务器端的配置（Linux）

    MEDIA_MOUNT_1 = "/fileData/fileData_master/ST/"         # 主文件服务器挂载盘
    MEDIA_MOUNT_2 = "/fileData/fileData_slave/ST/"          # 挂载文件服务器备用盘


class FilePath(object):
    def __init__(self):
        self.path1 = MEDIA_MOUNT_1
        self.path2 = MEDIA_MOUNT_2
        self.path3 = None
        self._stop = False
        self.root_url = {self.path1: "/fileData/fileData_master/ST/",
                         self.path2: "/fileData/fileData_slave/ST/"}

    def fun1(self):
        # time.sleep(5000)
        if os.path.exists(self.path1):
            self.path3 = self.path1
        return

    def fun2(self):
        # time.sleep(0.3)
        if os.path.exists(self.path2):
            if not self.path3:
                self.path3 = self.path2
        else:
            self._stop = True                       # 当检测到备用服务器都不存在时，结束检测
        return

    def run(self):
        self.t1 = threading.Thread(target=self.fun1)
        self.t1.setName("t1")
        self.t1.start()
        # self.t1.join(2)

        self.t2 = threading.Thread(target=self.fun2)
        self.t2.setName("t2")
        self.t2.start()

        while self._stop is False:
            if self.path3:
                return self.path3

        return None                              # 文件服务器全都不生效时返回None

    def get_root_url(self):
        return self.root_url[self.path3]
